package com.wwp.tbshop.model.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.LimitBuyActivity;
import com.wwp.tbshop.model.bean.LimitBuyBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;
import com.wwp.tbshop.util.utils.TimeTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${wenping} on 6/16/2017.
 */

public class LimitBuyAdapater extends RecyclerView.Adapter {

    private SparseArray<CountDownTimer> countDownMap;

    private final Context mContext;
    private LimitBuyActivity mLimitBuyActivity;
    private CountDownTimer countDownTimer;

    public LimitBuyAdapater(LimitBuyActivity limitBuyActivity, Context context) {
        mContext = context;
        mLimitBuyActivity = limitBuyActivity;
        countDownMap = new SparseArray<>();
    }

    private List<LimitBuyBean.ProductListBean> mProductListBeanList = new ArrayList<>();

    public void setProductListBeanList(List<LimitBuyBean.ProductListBean> productListBeanList) {
        mProductListBeanList = productListBeanList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_limit_buy, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(mProductListBeanList.get(position));
    }

    private static final String TAG = "LimitBuyAdapater";

    @Override
    public int getItemCount() {
        if (mProductListBeanList != null) {
            Log.e(TAG, "getItemCount ");
            return mProductListBeanList.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public CountDownTimer countDownTimer;
        ImageView mItemLimitBuyGoodsiv;
        TextView mItemLimitBuyName;
        TextView mItemLimitBuyPrice;
        TextView mItemLimitBuyMarketprice;
        TextView mItemLimitBuyLeftTime;
        private int time;

        public ViewHolder(View view) {
            super(view);
            mItemLimitBuyGoodsiv = (ImageView) view.findViewById(R.id.item_limit_buy_goodsiv);
            mItemLimitBuyName = (TextView) view.findViewById(R.id.item_limit_buy_name);
            mItemLimitBuyPrice = (TextView) view.findViewById(R.id.item_limit_buy_price);
            mItemLimitBuyMarketprice = (TextView) view.findViewById(R.id.item_limit_buy_marketprice);
            mItemLimitBuyLeftTime = (TextView) view.findViewById(R.id.item_limit_buy_lefttime);
        }

        public void setData(final LimitBuyBean.ProductListBean bean) {

            Picasso.with(UiUtil.getContext())
                    .load(Constant.HOST + bean.getPic())
                    .placeholder(R.drawable.pic_default_grid)
                    .error(R.drawable.ic_error_page)
                    .into(mItemLimitBuyGoodsiv);
            mItemLimitBuyName.setText(bean.getName() + "");
            mItemLimitBuyMarketprice.setText("¥" + bean.getPrice() + "");
            mItemLimitBuyMarketprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//贯穿线
            mItemLimitBuyPrice.setTextColor(Color.RED);
            mItemLimitBuyPrice.setText("¥" + bean.getLimitPrice() + "");

            time = (bean.getLeftTime());
            mItemLimitBuyLeftTime.setText(TimeTools.getCountTimeByLong( time*1000));
        }
    }
    /**
     * 取消倒计时
     */
    public void stopCountDown() {
        countDownTimer.cancel();
    }
    /**
     * 开始倒计时
     */
    public void startCountDown(){
        countDownTimer = new CountDownTimer(Integer.MAX_VALUE,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
              //一秒钟触发一次
                for (LimitBuyBean.ProductListBean listBean : mProductListBeanList) {
                    int leftTime = listBean.getLeftTime();
                    if (leftTime < 0) {
                        leftTime = 0;
                    } else {
                        leftTime--;
                    }
                    listBean.setLeftTime(leftTime);
                }
                notifyDataSetChanged();
            }

            @Override
            public void onFinish() {

            }
        }.start();
    }
}


























































































